﻿using System;
using System.IO;
using System.Windows.Forms;

namespace TextFileMerger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnSelectFile1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFile1.Text = openFileDialog.FileName;
            }
        }

        private void btnSelectFile2_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFile2.Text = openFileDialog.FileName;
            }
        }

        private void btnMergeFiles_Click_1(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtFile1.Text) || string.IsNullOrWhiteSpace(txtFile2.Text))
            {
                MessageBox.Show("请先选择两个文件！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                // 读取第一个文件内容
                string content1 = File.ReadAllText(txtFile1.Text);

                // 读取第二个文件内容
                string content2 = File.ReadAllText(txtFile2.Text);

                // 合并内容
                string mergedContent = content1 + Environment.NewLine + content2;

                // 确保Data目录存在
                string dataDirectory = Path.Combine(Application.StartupPath, "Data");
                if (!Directory.Exists(dataDirectory))
                {
                    Directory.CreateDirectory(dataDirectory);
                }

                // 创建新文件路径
                string outputFileName = $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt";
                string outputPath = Path.Combine(dataDirectory, outputFileName);

                // 写入合并后的内容
                File.WriteAllText(outputPath, mergedContent);

                MessageBox.Show($"文件合并成功！\n保存位置: {outputPath}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"合并文件时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}